<script setup>
import { computed, ref, provide, h } from 'vue'
import MyTable from '@/components/MyTable/MyTable.vue'
import { Modal } from 'ant-design-vue'
import { message } from 'ant-design-vue'
import api from '@/api/index.js'

const emits = defineEmits(['pageChange', 'xq', 'reload'])
const props = defineProps({
  // 表格数据总条数
  total: { type: Number, default: 0 },
  // 表格当前页码
  pageNum: { type: Number, default: 1 }
})
// 分页参数
const pagination = computed(() => ({
  total: props.total,
  showSizeChanger: false,
  current: props.pageNum,
  onChange: (e) => {
    emits('pageChange', e)
  }
}))

// 表格列配置
const columns = ref([
  { ellipsis: false, title: '商品名称', dataIndex: 'productName', key: 'productName' },
  { ellipsis: false, title: '规格型号', dataIndex: 'specs', key: 'specs' },
  { ellipsis: false, title: '计量单位', dataIndex: 'unit', key: 'unit' },
  { ellipsis: false, title: '库存', dataIndex: 'currentSell', key: 'currentSell' },
  { ellipsis: false, title: '操作', key: 'set', width: '200px', align: 'center' }
])

// 数据详情
const xq = (item) => {
  emits('xq', item)
}

//删除数据
const del = (item) => {
  Modal.warning({
    title: '删除农资数据',
    content: h('span', {}, '删除后起所有数据都将被清空'),
    okText: '删除',
    okCancel: true,
    okButtonProps: { type: 'primary', danger: true },
    cancelButtonProps: { type: 'primary' },
    cancelText: '取消',
    onOk: async () => {
      await api.delCs({ id: item.id })
      message.success('删除成功')
      emits('reload')
      return Promise.resolve()
    },
    onCancel: () => {
      message.success('取消删除')
      return Promise.resolve()
    }
  })
}
provide('columns', columns.value)
</script>

<template>
  <MyTable :pagination="pagination" :scroll="{ y: 'calc(100vh - 383px)' }">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'set'">
        <AButton type="link" @click="xq(record)">详情</AButton>
        <!-- <AButton type="link" class="com-delbtn" @click="del(record)">删除</AButton> -->
      </template>
    </template>
  </MyTable>
</template>